package com.app.controller;

import com.app.model.Banner;
import com.app.page.CommonPage;
import com.app.request.PageParamRequest;
import com.app.result.CommonResult;
import com.app.service.IBannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 轮播图管理	 前端控制器
 * </p>
 *
 * @author ghoul
 * @since 2024-12-27
 */
@RestController
@RequestMapping("/banner")
public class BannerController {
    @Autowired
    private IBannerService bannerService;

    /**
     * 列表
     *
     * @param request
     * @param pageParamRequest
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult<CommonPage<Banner>> getList(@Validated Banner request, @Validated PageParamRequest pageParamRequest) {
        return CommonResult.success(CommonPage.restPage(bannerService.getAdminList(request, pageParamRequest)));
    }

    /**
     * 列表
     * @return
     */
    @RequestMapping(value = "/small/list", method = RequestMethod.GET)
    public CommonResult<List<Banner>> getSmallList() {
        return CommonResult.success(bannerService.getSmallList());
    }

    /**
     * 新增
     *
     * @param banner
     * @return
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public CommonResult<String> save(@RequestBody @Validated Banner banner) {
        if (bannerService.create(banner)) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }


    /**
     * 删除
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public CommonResult<String> delete(Integer id) {
        if (bannerService.deleteById(id)) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    public CommonResult<Banner> info(Integer id) {
        return CommonResult.success(bannerService.getDetail(id));
    }

    /**
     * 修改状态
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/updateStatus", method = RequestMethod.GET)
    public CommonResult<String> updateStatus(Integer id, boolean status) {
        if (bannerService.updateStatus(id, status)) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }
}

